﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.StorageClient;
using nwrole.Azure;
using nwrole.lib;

namespace nwrole.Supervisor.WorkerRole {
    public class WorkerRole : RoleEntryPoint {
        //private bool running = false;
        //private bool stopped = false;

        //private nwrole.lib.SupervisorHost supervisorHost;

        private WorkerRoleBase workerRoleBase = new WorkerRoleBase();

        public override void Run() {
            // This is a sample worker implementation. Replace with your logic.
            
            workerRoleBase.Run();

            /*if(RoleEnvironment.IsAvailable) {
                string test = RoleEnvironment.GetConfigurationSettingValue("test");
            }*/

        }

        public override bool OnStart() {

            if (!workerRoleBase.OnStart())
                return false;

            return base.OnStart();
        }

        public override void OnStop() {
            workerRoleBase.OnStop();

            base.OnStop();
        }

        /*private void GetServices() {
            // Setup the connection to Windows Azure Storage
            var storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=<YOURACCOUNTNAME>;AccountKey=<YOURACCOUNTKEY>");
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
 
            // For large file copies you need to set up a custom timeout period
            // and using parallel settings appears to spread the copy across multiple threads
            // if you have big bandwidth you can increase the thread number below
            // because Azure accepts blobs broken into blocks in any order of arrival.
            blobClient.Timeout = new System.TimeSpan(1,0,0);
            blobClient.ParallelOperationThreadCount = 2;
 
            // Get and create the container
            CloudBlobContainer blobContainer = blobClient.GetContainerReference("services");
            blobContainer.CreateIfNotExist();
 
            // Setup the permissions on the container to be public
            var permissions = new BlobContainerPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Off;
            blobContainer.SetPermissions(permissions);
 
            // Make a unique blob name
            string path = "E:\\FIlms\\Edible\\Cerne_Egg_Project.wmv";
            string extension = System.IO.Path.GetExtension(path);
            string filename = System.IO.Path.GetFileName(path);
 
            //Open the stream and read it back.
            using (FileStream fs = File.OpenRead(path))
            {
                // Create the Blob and upload the file
                var blob = blobContainer.GetBlobReference(Guid.NewGuid().ToString() + "/" + filename);
                blob.UploadFromStream(fs);    
                
                // Set the metadata into the blob
                blob.Metadata["FileName"] = filename;
                blob.Metadata["Submitter"] = "Automated Encoder";
                blob.SetMetadata();
 
                // Set the properties
                blob.Properties.ContentType = "video/x-ms-wmv";
                blob.SetProperties();
            }
        }*/
    }
}
